﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;
using System.Data.SqlTypes;

namespace IHISCETeim.DAL
{
    public class OMailLineDAL
    {
        public static int AddOMailLine(OMailLineInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OMailLine_AddOMailLine", obj.SerialNumber, obj.MachineType, obj.GPSNumber, obj.EngineNumber);
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }

        public static bool DeleteOMailLine(int MLID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OMailLine_DeleteOMailLine", MLID);
            try
            {
                int result = Convert.ToInt32(DB.ExecuteScalar(dbCommand));
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static IDataReader GetOMailLineById(int MLID)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("OMailLine_GetOMailLineById", MLID);
            return DB.ExecuteReader(dbCommand);
	    }

        public static DataTable GetOMailLinePagination(string whereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("OMailLine_GetOMailLinePagination");
            DB.AddInParameter(dbCommand, "whereSql", DbType.String,whereSql);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32,PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32,PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String,Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32,8);
			DataTable dt=  DB.ExecuteDataSet(dbCommand).Tables[0];
				
            TotalCount=Convert.ToInt32( DB.GetParameterValue(dbCommand,"TotalCount"));
            return dt;
	    }

        public static bool UpdateOMailLine(OMailLineInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OMailLine_UpdateOMailLine", obj.MLID, obj.SerialNumber, obj.MachineType, obj.GPSNumber, obj.EngineNumber);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
        public static int AddOMailLineDetail(OMailLineDetailInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OMailLineDetail_AddOMailLineDetail", obj.MLID, obj.GW, obj.GWName, obj.ChceckInclude, obj.InspectionStandards, obj.TestMethod, obj.SelfCheckResult, obj.SelfCheckPeople, obj.SelfCheckDate, obj.SamplingResult, obj.SamplingPeople, obj.SamplingDate, obj.SamplingRemark, obj.MTTCode);
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }

        public static bool UpdateOMailLineDetail(OMailLineDetailInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OMailLineDetail_UpdateOMailLineDetail", obj.MLDID, obj.SelfCheckResult, obj.SelfCheckPeople, obj.SelfCheckDate, obj.SamplingResult, obj.SamplingPeople, obj.SamplingDate, obj.SamplingRemark);
            try
            {
                return Convert.ToInt32(DB.ExecuteScalar(dbCommand)) > 0;
            }
            catch (Exception exc)
            {
                return false;
            }
        }

        public static IDataReader GetOMailLineDetailByMLID(int MLID)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("OMailLineDetail_GetOMailLineDetailByMLID", MLID);
            return DB.ExecuteReader(dbCommand);
	    }

        public static bool UpdateOMailLineMaxDate(int MLID)
        {
            try
            {
                Database DB = DatabaseFactory.CreateDatabase();

                DbCommand dbCommand = DB.GetStoredProcCommand("OMailLineDetail_UpdateOMailLineMaxDate", MLID);
                DB.ExecuteNonQuery(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static IDataReader GetAllOMailLineTemplete()
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("OMailLineTemplete_GetAllOMailLineTemplete");
            return DB.ExecuteReader(dbCommand);
	    }
        public static bool UpdateOMailLineTemplete(OMailLineTempleteInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OMailLineTemplete_UpdateOMailLineTemplete", obj.MLTID, obj.GW, obj.GWName, obj.ChceckInclude, obj.InspectionStandards, obj.TestMethod, obj.SelfCheckPeople, obj.SamplingResult, obj.SamplingPeople, obj.MTTCode);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }

        #region 报表
        /// <summary>
        /// 按工位统计NG量报表
        /// </summary>
        /// <param name="StateDate"></param>
        /// <param name="EndDate"></param>
        /// <returns></returns>
        public static DataTableCollection OMailLine_GetReport1(string StateDate, string EndDate)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OMailLine_GetReport1", StateDate, EndDate);
            return DB.ExecuteDataSet(dbCommand).Tables;
        }
        /// <summary>
        /// 按检查内容统计NG量报表
        /// </summary>
        /// <param name="StateDate"></param>
        /// <param name="EndDate"></param>
        /// <returns></returns>
        public static DataTableCollection OMailLine_GetReport2(string StateDate, string EndDate)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OMailLine_GetReport2", StateDate, EndDate);
            return DB.ExecuteDataSet(dbCommand).Tables;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="StateDate"></param>
        /// <param name="EndDate"></param>
        /// <returns></returns>
        public static DataTable OMailLine_GetMacTypeReport(string StateDate, string EndDate)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OMailLine_GetMacTypeReport", StateDate, EndDate);
            return DB.ExecuteDataSet(dbCommand).Tables[0];
        }
        #endregion


    }
}
